/**
 * 
 */
package com.logistics.web;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.logistics.Pickup;
import com.logistics.TimeTemplate;
import com.logistics.admin.BaseAction;
import com.logistics.admin.DateUtils;
import com.logistics.service.PickupService;
import com.logistics.service.TimeTemplateService;

/**
 * @author hzz
 *
 * 2014年3月31日
 */

@Component("pickupAction")
@Scope("prototype")
public class PickupAction extends BaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private PickupService service;
	
	@Autowired
	public void setService(PickupService service) {
		this.service = service;
	}
	
	private TimeTemplateService templateService;

	@Autowired
	public void setTemplateService(TimeTemplateService templateService) {
		this.templateService = templateService;
	}

	private Pickup pickup;

	public Pickup getPickup() {
		return pickup;
	}

	public void setPickup(Pickup pickup) {
		this.pickup = pickup;
	}
	
	private List<TimeTemplate> templates;
	
	public List<TimeTemplate> getTemplates() {
		return templates;
	}

	public void setTemplates(List<TimeTemplate> templates) {
		this.templates = templates;
	}

	public String save() {
		pickup.setPrint(true);
		pickup.setStatus(false);
		pickup.setCreateDate(new Date());
		service.save(pickup);
		return SUCCESS;
	}
	
	public String showAddView() {
		templates = templateService.queryAvailable(false);
		return SUCCESS;
	}
	
	public String pickupInfo() {
		if (StringUtils.isBlank(getId())) {
			return "showAdd";
		}
		pickup = service.getById(getId());
		
		return SUCCESS;
		
	}
	
	public void needPrint() {
		
		List<Pickup> pickups = service.needPrint();
		JSONObject jsonObject = new JSONObject();
		StringBuffer buffer = new StringBuffer();
		for (Pickup pickup : pickups) {
			if (StringUtils.isNotBlank(buffer.toString())) {
				buffer.append("," + pickup.getEntityId());
			} else {
				buffer.append(pickup.getEntityId());
			}
		}
		jsonObject.put("status", 200);
		jsonObject.put("ids", buffer.toString());
		outputString(jsonObject.toString());
	}
	
	public void printDetail() {
		if (StringUtils.isBlank(getId())) {
			outputInvalidStatus(500);
			return;
		}
		
		pickup = service.getById(getId());
		JsonConfig config = getJsonConfig();
		config.registerJsonValueProcessor("createDate", new JsonValueProcessor() {
			
			@Override
			public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {
				
				return DateUtils.formatDateTime((Date) arg1);
			}
			
			@Override
			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				return null;
			}
		});
		
		if (pickup == null || "0".equals(pickup.getEntityId())) {
			outputInvalidStatus(500);
			return;
		}
		pickup.setPrint(false);
		service.update(pickup);
		outputJsonObject(JSONObject.fromObject(pickup, config));
	}
	
	public static void main(String[] args) {
		System.out.println("啊岁登基萨拉肯德基啊岁肯德基里卡萨帝景萨拉空间的里撒娇的里撒啊岁登基萨拉肯德基啊岁肯德基里卡萨帝景萨拉空间的里撒娇的里撒".length());
	}
}
